<template>
  <div>
    <!-- 添加按钮 -->
    <el-card>
      <div slot="header">
        <el-button
          type="primary"
          size="medium"
          icon="el-icon-plus"
          @click="isDialogShow = true"
          >添加</el-button
        >
      </div>

      <!-- 展示表格 -->
      <el-table :data="trademarkList" border stripe>
        <el-table-column
          type="index"
          label="序号"
          width="80px"
          align="center"
        />
        <el-table-column label="品牌名称" prop="tmName" />
        <el-table-column label="品牌LOGO">
          <template v-slot="{ row }">
            <img :src="row.logoUrl" height="50" />
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button
              type="warning"
              size="small"
              icon="el-icon-edit"
              @click="edit(row)"
            ></el-button>
            <el-button
              type="danger"
              size="small"
              icon="el-icon-delete"
              @click="deleteTm(row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        :current-page.sync="currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size.sync="pageSize"
        layout=" prev, pager, next, sizes,total"
        :total="totalNum"
        background
      >
      </el-pagination>
    </el-card>

    <!-- 添加弹窗 -->
    <el-dialog
      title="添加品牌"
      :visible.sync="isDialogShow"
      width="60%"
      @close="resetForm"
    >
      <!-- 收集表单 -->
      <el-form
        :model="form"
        :rules="rules"
        label-width="80px"
        :inline="false"
        size="normal"
        ref="form"
      >
        <el-form-item label="品牌名称" label-width="100px" prop="tmName">
          <el-input v-model="form.tmName"></el-input>
        </el-form-item>
        <el-form-item label="品牌LOGO" label-width="100px">
          <el-upload
            class="avatar-uploader"
            :action="`${baseUrl}/admin/product/fileUpload`"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="form.logoUrl" :src="form.logoUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>

      <span slot="footer">
        <el-button @click="resetForm">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  reqTrademarkList,
  reqAddTrademark,
  reqModifyTrademark,
  reqDeleteTrademark
} from "@/api/trademark";
export default {
  name: "trademark",
  components: {},
  data() {
    return {
      currentPage: 1,
      pageSize: 5,
      totalNum: 0,
      trademarkList: [],
      isDialogShow: false,
      form: {
        tmName: "",
        logoUrl: ""
      },
      baseUrl: process.env.VUE_APP_BASE_API,
      rules: {
        tmName: [
          {
            required: true,
            message: "请输入品牌名称",
            trigger: "blur"
          }
        ]
      }
    };
  },
  methods: {
    // 请求品牌列表
    async getTrademarkList() {
      const re = await reqTrademarkList(this.currentPage, this.pageSize);
      this.totalNum = re.data.total;
      this.trademarkList = re.data.records;
    },
    // 点击关闭弹窗,重置表单数据
    resetForm() {
      this.isDialogShow = false;
      this.form = {
        tmName: "",
        logoUrl: ""
      };
      this.$refs.form.clearValidate();
    },
    // 图片上传成功
    handleAvatarSuccess(res) {
      this.form.logoUrl = res.data;
    },
    // 图片上传的校验
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 < 50;

      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 50KB!");
      }
      return isLt2M;
    },
    // 提交
    submit() {
      this.$refs.form.validate(async v => {
        if (v) {
          if (this.form.id) await reqModifyTrademark(this.form);
          else await reqAddTrademark(this.form);
          this.$message.success("保存成功");
          this.getTrademarkList();
          this.resetForm();
        } else {
          this.$message.error("请检查表单是否正确填写");
          return false;
        }
      });
    },
    // 点击修改回填数据
    edit(form) {
      this.form = form;
      this.isDialogShow = true;
    },
    //删除品牌
    async deleteTm(id) {
      await reqDeleteTrademark(id);
      this.getTrademarkList();
    }
  },
  mounted() {
    this.getTrademarkList();
  },
  watch: {
    currentPage() {
      this.getTrademarkList();
    },
    pageSize() {
      this.getTrademarkList();
    }
  }
};
</script>

<style lang="scss">
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
